﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using System.Xml.Serialization;

namespace YouHP.BLL.MedicalGraph
{
    [XmlRoot("Graph")]
    public class GraphRule
    {
        [XmlIgnore]
        public int Type {set; get;}

        [XmlIgnore]
        public int Height = 300;

        [XmlIgnore]
        public int LegendWidth = 300;

        [XmlIgnore]
        public int DataWidth = 600;

        [XmlIgnore]
        public int mMinCount;

        [XmlIgnore]
        public int mGridCount = 20;

        [XmlIgnore]
        public int mYAxisType;

        [XmlIgnore]
        public Pen mGridPen {get; set;}

        [XmlIgnore]
        public Pen mXAxisPen { get; set; }

        [XmlIgnore]
        public Font mGridFont { get; set; }

        [XmlIgnore]
        public StringFormat mLabelFotmat { get; set; }

        [XmlIgnore]
        public DateTime MaxDateTime { get; set; }

        [XmlIgnore]
        public DateTime MinDateTime { get; set; }

        [XmlIgnore]
        public string Prefix;

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("Message")]
        public string Message {get; set;}


        public GraphRule()
        {
            Height = 300;
            LegendWidth = 300;
            DataWidth = 600;

            mGridPen = new Pen(Color.Gray,0.5f);
            mGridPen.DashStyle = DashStyle.Dot;

            mXAxisPen = new Pen(Color.Black, 2f);
            mXAxisPen.CustomEndCap = new AdjustableArrowCap(4.0f, 6.0f);
            mGridFont = new Font("宋体", 8.0f);

            //mVariables = new List<GraphVariable>();
            mLabelFotmat = new StringFormat();
            mLabelFotmat.Alignment = StringAlignment.Center;
            mLabelFotmat.LineAlignment = StringAlignment.Near;

            Name = "新建图表";
        }

        [XmlIgnore]
        public Brush RangeBrush {get; set;}

        public void SetRangeColor(Color color)
        {
            RangeBrush = new SolidBrush(color);
        }


        public class Variable
        {
            public string mName;
            public string mKey;
            public string ShortName;
            public string Unit;
            public List<double> GridValue = new List<double>();           
         

            

            public List<double> mUpper = new List<double>();
            public List<double> mLower = new List<double>();
            //public List<Brush> mRangBrush = new List<Brush>();
            public List<string> mRangeName = new List<string>();

            public void AddRange(double lower, double upper)
            {
                mLower.Add(lower);
                mUpper.Add(upper);
            }
            public void SetRange0(double lower, double upper)
            {
                if (mLower.Count < 1) mLower.Add(lower);
                else mLower[0] = lower;

                if (mUpper.Count < 1) mUpper.Add(upper);
                else mUpper[0] = upper;
            }
           
            internal double GetRelPosition(double value)
            {
                List<double> vallist = GridValue;
                double retval = 0.0;

                double lastval = vallist[0];
                if (value < lastval || Math.Abs(lastval - value) < 0.001)
                {
                    retval = value / lastval;
                }
                else
                {
                    retval = 1.0;
                    int cnt = vallist.Count;
                    lastval = vallist[0];
                    for (int i = 1; i < cnt; i++)
                    {
                        double val = vallist[i];
                        if (value < val || Math.Abs(val - value) < 0.001)
                        {
                            retval += (value - lastval) / (val - lastval);
                            break;
                        }
                        else
                        {
                            retval += 1.0f;
                            lastval = val;
                        }
                    }
                }

                retval /= vallist.Count;
                return retval;
            }

           
            double mNormalPosition;
            double mNewPercent;
            double mNormalValue;

            public double GetNormalPosition()
            {
                mNormalValue = this.mUpper[0];
                mNormalPosition = GetRelPosition(mNormalValue);
                return mNormalPosition;
            }
            public void SetNormalPosition(double positon)
            {
                mNewPercent = positon;

            }

            public double GetRelPosition2(double value)
            {
                double percent = GetRelPosition(value);
                if (value < mNormalValue) return (percent / mNormalPosition * mNewPercent);
                else return (percent - mNormalPosition) / (1 - mNormalPosition) * (1 - mNewPercent) + mNewPercent;
            }
        }

        public class Group
        {
            public string mName;
            public List<Variable> mVariables = new List<Variable>();

            internal void AddVariable(Variable var)
            {
                mVariables.Add(var);
            }
        }

        public List<Group> mGroups = new List<Group>();
        public int AddGroup(string groupname)
        {
            Group group = new Group();
            group.mName = groupname;
            mGroups.Add(group);
            return mGroups.Count - 1;
        }
        public void AddVariable(int idx, string name, string key)
        {
            Variable var = new Variable();
            var.mName = name;
            var.mKey = key;
            mGroups[idx].AddVariable(var);
        }
        public void AddVariable(int idx, Variable var)
        {
            mGroups[idx].AddVariable(var);
        }


        //用户一般情况和实验室指标
        public Variable GetVariable(int idx)
        {
            return mGroups[0].mVariables[idx];
        }
        public int GetVariableCount()
        {
            return mGroups[0].mVariables.Count;
        }
        public int GetGroupCount()
        {
            return mGroups.Count;
        }

        internal void AddGroup(Group group)
        {
            mGroups.Add(group);
        }
    }

  
}
